package com.sino.operation.controller;

import com.adtec.ms.msagent.service.AddressingService;
import com.adtec.ms.msagent.service.AgentMngService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sino.operation.vo.RegSvrAddrVO;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName EscController
 * @Description esc服务接口
 * @Author yangfangyu
 * @Date 2021/9/18 9:20
 * @Version 1.0
 **/
@RestController
@RequestMapping("/esc")
@Log4j2
@PropertySource("classpath:param.properties")
public class EscController {


    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    Environment env;

    /**
     * @Author fangyu_yang
     * @Description 获取AddressingService
     * @Date 17:30 2021/9/14
     * @Param []
     * @return com.adtec.ms.msagent.service.AddressingService
     **/
    public AddressingService getApplicationContext(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        AddressingService addressingService= (AddressingService) ctx.getBean("addressingService");
        return addressingService;
    }



    /**
     * @Author fangyu_yang
     * @Description 服务注册
     * @Date 17:28 2021/9/14
     * @Param [svrName, url]
     * @return boolean
     **/
    @PostMapping("regSvrAddr")
    public Map<String,Object> regSvrAddr( @ApiParam(value = "服务信息",required = true) @RequestBody RegSvrAddrVO regSvrAddrVO){
        log.info("服务注册开始-regSvrAddr，参数："+regSvrAddrVO);
        AddressingService addressingService = getApplicationContext();
        HashMap<String, Object> regServicesInfoMap = Maps.newHashMap();
        ArrayList<Map<String, Object>> allServices = Lists.newArrayList();
        HashMap<String, Object> serInfo = Maps.newHashMap();
        HashMap<String, Object> protocolMap = Maps.newHashMap();
        //token存放在redis
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String escToken = ops.get("EscToken");
        regServicesInfoMap.put("Token",escToken);
        serInfo.put("SvrName",regSvrAddrVO.getSvrName());
        serInfo.put("Name",regSvrAddrVO.getName());
        serInfo.put("EnName",regSvrAddrVO.getEnName());
        serInfo.put("PartId",env.getProperty("PartId"));
        serInfo.put("TimeOut",env.getProperty("TimeOut"));
        serInfo.put("ProtocolType",env.getProperty("ProtocolType"));

        protocolMap.put("HostName",env.getProperty("HostName"));
        protocolMap.put("Port",env.getProperty("Port"));
        protocolMap.put("IfKeepAlive",env.getProperty("IfKeepAlive"));
        protocolMap.put("URL",regSvrAddrVO.getUrl());

        serInfo.put("COM_HTTP",protocolMap);
        allServices.add(serInfo);
        regServicesInfoMap.put("Services",allServices);

        Map<String,Object> regServicesResultMap =Maps.newHashMap();
        try {
            regServicesResultMap = addressingService.regSvrAddr(regServicesInfoMap);
        }catch (Exception exception){
            log.info("服务注册异常-regSvrAddr,"+exception.toString());
            exception.printStackTrace();
        }
        log.info("服务注册结束-regSvrAddr");
        return regServicesResultMap;
    }


    /**
     * @Author fangyu_yang
     * @Description 服务注销
     * @Date 17:45 2021/9/14
     * @Param [svrName]
     * @return boolean
     **/
    @PostMapping("cancelSvrAddr")
    public HashMap<String, Object> cancelSvrAddr(@ApiParam(value = "服务信息",required = true) @PathVariable List<String> instIds){
        log.info("服务注销开始-cancelSvrAddr，参数："+instIds);
        AddressingService applicationContext = getApplicationContext();
        //token存放在redis
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String escToken = ops.get("EscToken");
        HashMap<String, Object> cancelSvrAddrMap = Maps.newHashMap();
        try {
            cancelSvrAddrMap = applicationContext.cancelSvrAddr(escToken, instIds);
        }catch (Exception exception){
            log.info("服务注销异常-cancelSvrAddr"+exception.toString());
            exception.printStackTrace();
        }
        log.info("服务注销结束-cancelSvrAddr");
        return cancelSvrAddrMap;
    }


    /**
     * @Author fangyu_yang
     * @Description agent服务停止
     * @Date 15:27 2021/9/18
     * @Param []
     * @return java.util.HashMap<java.lang.String,java.lang.Object>
     **/
    @PostMapping("stopAgent")
    public HashMap<String, Object> stopAgent(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        AgentMngService agentMngService = (AgentMngService) ctx.getBean("agentMngService");
        //token存放在redis
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String escToken = ops.get("EscToken");
        ArrayList tokeList = Lists.newArrayList();
        tokeList.add(escToken);
        HashMap<String, Object> hashMap = agentMngService.stopAgent(tokeList);
        return hashMap;
    }


}
